package net.imagine.backend.mockrmi.local;

import net.imagine.backend.mockrmi.ServiceBinder;
import net.imagine.backend.mockrmi.ServiceRegistry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 本地的ServiceBinder实现，不需要通过TCP/IP，单机时提示效率
 * 
 * @see ServiceBinder
 * @author wuqh
 */
public class LocalServiceBinder implements ServiceBinder {
	private static final Log logger = LogFactory.getLog(LocalServiceBinder.class);

	@Override
	public void bind(int port, String id, Object target) {
		if (ServiceRegistry.lookup(id) != null) {
			logger.warn("can not bind as the service has exist at the server");
		} else {
			rebind(port, id, target);
		}
	}

	@Override
	public void rebind(int port, String id, Object target) {
		this.bind(port, id, target);
		logger.debug("in local never use dport, just use id");
		ServiceRegistry.regist(id, target);
	}

	@Override
	public void unbind(int port, String id) {
		ServiceRegistry.unbind(id);
	}

}
